package kr.saeildamil.androidstudy.ch12;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerTest extends Activity {
	ArrayAdapter<CharSequence> adspin;
	boolean mInitSpinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_spinnertest);

		Spinner spin = (Spinner) findViewById(R.id.ch12_spinnertest_spin);
		spin.setPrompt("나라를 선택하세요");

		adspin = ArrayAdapter.createFromResource(this, R.array.country,
				android.R.layout.simple_spinner_dropdown_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spin.setAdapter(adspin);
		
		/*spin.setSelection(5);    //순서중 몇번째를 보이게끔 지정해줄때
		int pos = spin.getSelectedItemPosition();*/
		
		spin.setOnItemSelectedListener(new OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				if (mInitSpinner == false) {
					mInitSpinner = true;
					return;
				}
				Toast.makeText(SpinnerTest.this, adspin.getItem(position),
						Toast.LENGTH_SHORT).show();
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent){}
		});
	}
}
